/*******************************************************************************
* This class is used to open video files. It uses ffmpeg libraries for video
* files.
* 
*
* To compile this code, you must add the libs -lavformat, -lavcodec, -lavutil,
* lswscale to g++ compiler.
* 
*
* Licensed under GPL Version 3 license (/license.txt).
* Author: Eder A. Perez (eder.perez@ice.ufjf.br)
*******************************************************************************/


#ifndef __CV_VIDEOFILE_HANDLER__
#define __CV_VIDEOFILE_HANDLER__

#include "../cvdatatypes.h"
#include "../cverror.h"
#include "../libav.h"



class CVVideoFile {

  /**********************
  *      ATTRIBUTES     *
  **********************/

  public:
    // Format attributes
    unsigned int width;
    unsigned int height;
    float        fps;
    unsigned int bpp;        // Bits per pixel
    unsigned int bitrate;    // In bits/second
    CVpixel_fmt_t pxlfmt;    // Pixel format for frame grabbed


    // For error handle
    CVerror_t errorcode;

  private:
    CVubyte_t  *buffer;        // Store a frame grabbed
    int buffsize;
    bool input_opened;       // True if a video file is already opened
   
    // Libav attributes
    int             v_strm;
    AVFormatContext *fmt_ctx;
    AVCodecContext  *codec_ctx;
    AVFrame         *frame;
    AVFrame         *frameConv;   // Hold converted original frame
    SwsContext      *conv_ctx;    // Context to convert pixel formats



  /**********************
  *       METHODS       *
  **********************/
  
  public:
    // Constructors
    CVVideoFile(CVpixel_fmt_t output_pxl_fmt);
    ~CVVideoFile();
    
    // Methods
    bool open(const char* path);
    bool getFrame(CVubyte_t* data);
    bool resize(int width, int height);
    int  createBuffer(CVubyte_t **buffer);
    //CVImage* createCVImage();
    
  private:
    bool convert_context(int width, int height);
    int get_bpp();
    enum PixelFormat get_libav_pxl_fmt();

};

#endif

